package com.wzcl.app.service.impl;

import com.wzcl.app.dao.model.AppUser;
import com.wzcl.app.dao.model.UserCourse;
import com.wzcl.app.dao.repository.AppUserRepository;
import com.wzcl.app.dao.repository.CourseRepository;
import com.wzcl.app.dao.repository.UserCourseRepository;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.StudentInfoDto;
import com.wzcl.app.service.ImportStudentService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author beingven
 * @Description TODO
 * @createTime 2020年11月14日 08:23:00
 */
@Service
public class ImportStudentServiceImpl implements ImportStudentService {
    @Resource
    private AppUserRepository appUserRepository;
    @Resource
    private CourseRepository courseRepository;
    @Resource
    private UserCourseRepository userCourseRepository;
    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public RVO getImportStudents(List<StudentInfoDto> studentList) {
        UserCourse saveUserCourse = null;
        for (int index = 0; index < studentList.size(); index++) {
            AppUser appUser = appUserRepository.findByPhone(studentList.get(index).getPhone());
            AppUser app = new AppUser();
            UserCourse userCourse = new UserCourse();
            // 没有该用户，则新建用户并绑定课程
            if (appUser == null){
                //对象转换
                app.setNickName(studentList.get(index).getName());
                app.setRealName(studentList.get(index).getName());
                app.setPhone(studentList.get(index).getPhone());
                //设置默认密码并加密
                app.setPassword(passwordEncoder.encode("123456"));
                app.setScore(BigDecimal.valueOf(0.00));
                app.setSex(2);
                //默认账户未被注销且未被禁用
                app.setState(0);
                app.setUserState(0);
                //创建用户
                appUserRepository.save(app);
                //获取课程id并添加用户与课程的绑定
                userCourse.setUserId(appUserRepository.findByPhone(studentList.get(index).getPhone()).getId());
                userCourse.setUserPhone(studentList.get(index).getPhone());
                userCourse.setCourse(courseRepository.findCourseByCourseName(studentList.get(index).getCourse()));
                saveUserCourse = userCourseRepository.save(userCourse);
            }else {
                //直接添加课程
                userCourse.setUserId(appUserRepository.findByPhone(studentList.get(index).getPhone()).getId());
                userCourse.setUserPhone(studentList.get(index).getPhone());
                userCourse.setCourse(courseRepository.findCourseByCourseName(studentList.get(index).getCourse()));
                saveUserCourse = userCourseRepository.save(userCourse);
            }

        }
        if (saveUserCourse != null){
            return RVO.success("课程添加成功");
        }
        return RVO.error("发送数据为空");
    }
}
